package com.jinhua.huaojbackendjudgeservice.judge.strategy;


import com.jinhua.huaojbackendmodel.model.codesandbox.JudgeInfo;
import com.jinhua.huaojbackendmodel.model.entity.JudgeConfig;
import com.jinhua.huaojbackendmodel.model.entity.JudgeContext;
import com.jinhua.huaojbackendmodel.model.enums.QuestionSubmitJudgeInfoMessageEnum;

import java.util.List;

public class JavajudgeStrategy implements JudgeStrategy {
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
//        判断逻辑：  1. 查看输出是否和输入长度一样（一个输入应有一个输出）
        List<String> excuteOutputList = judgeContext.getExcuteOutputList();
        List<String> expectOutputList = judgeContext.getExpectOutputList();
        QuestionSubmitJudgeInfoMessageEnum judgeInfoMessageEnum = QuestionSubmitJudgeInfoMessageEnum.ACCEPTED ;
        if (excuteOutputList.size() != expectOutputList.size()) {
            judgeInfoMessageEnum = QuestionSubmitJudgeInfoMessageEnum.WRONG_ANSWER;
        }
//                  2.一次查看输出是否和预期一致
        for (int i = 0; i < expectOutputList.size(); i++) {
            if (!expectOutputList.get(i).equals(excuteOutputList.get(i))){
                judgeInfoMessageEnum = QuestionSubmitJudgeInfoMessageEnum.WRONG_ANSWER;
                break;
            }
        }

//                  判断题目的限制是否符合要求
        JudgeConfig judgeConfig = judgeContext.getJudgeConfig();
//        todo java 原生未实现内存
//        Long memoryLimit = judgeConfig.getMemoryLimit();
        Long timeLimit = judgeConfig.getTimeLimit();

        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        Long time = judgeInfo.getTime();
        Long memory = judgeInfo.getMemory();

        final long JAVA_EXTRA_TIME = 2000l;

        if (time > timeLimit + JAVA_EXTRA_TIME){
            judgeInfoMessageEnum = QuestionSubmitJudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;

        }

//        if (memory > memoryLimit){
//            judgeInfoMessageEnum = QuestionSubmitJudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
//        }



        judgeInfo.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfo;
    }
}
